/**
 * Bakalarska prace - Inteligentni budik
 * @package sleepCatcher
 * @file SleepCatcher.cpp
 * @author Jan Pesava - xpesav00
 * @email xpesav00@stud.fit.vutbr.cz
 * @date 24. 12. 2011
 */

#include "SleepCatcher.h"
#include "SCSettings.h"
#include "SCNavigator.h"

SleepCatcher::SleepCatcher()
{
}

SleepCatcher::~SleepCatcher()
{
}

Application*
SleepCatcher::CreateInstance(void)
{
	// Create the instance through the constructor.
	return new SleepCatcher();
}

bool
SleepCatcher::OnAppInitializing(AppRegistry& appRegistry)
{
	//Initialize
	SCDatabase::Construct();
	SCSettings::Construct();
	SCNavigator::Construct();
	SCFormManager::Construct();

	//Set start form
	SCFormManager::SetStarterForm(SCFormManager::IDF_ALARM , null);

	return true;
}

bool
SleepCatcher::OnAppTerminating(AppRegistry& appRegistry, bool forcedTermination)
{
	//Dealloc
	SCDatabase::Destruct();
	SCSettings::Destruct();
	SCNavigator::Destruct();
	SCFormManager::Destruct();

	return true;
}

void
SleepCatcher::OnForeground(void)
{
	if(SCFormManager::GetCurrentFormId() == SCFormManager::IDF_CATCH_SLEEP)
	{
		PowerManager::KeepScreenOnState(true, true);
		PowerManager::SetScreenBrightness(10);
	}

	//app is on foreground
	SCFormManager::screenSaver = false;
	SCFormManager::appOnBackground = false;
}

void
SleepCatcher::OnBackground(void)
{
	if(SCFormManager::GetCurrentFormId() == SCFormManager::IDF_CATCH_SLEEP)
	{
		PowerManager::KeepScreenOnState(false, false);

		if(SCFormManager::screenSaver)
		{
			SCFormManager::GetCurrentForm()->SetShowState(true);
			SCFormManager::GetCurrentForm()->RequestRedraw(true);
		}
	}

	//app is on background
	SCFormManager::screenSaver = false;
	SCFormManager::appOnBackground = true;
}

void
SleepCatcher::OnLowMemory(void)
{
	// TODO:
	// Free unused resources or close the application.
}

void
SleepCatcher::OnBatteryLevelChanged(BatteryLevel batteryLevel)
{
	// TODO:
	// Handle any changes in battery level here.
	// Stop using multimedia features(camera, mp3 etc.) if the battery level is CRITICAL.
}

void
SleepCatcher::OnScreenOn (void)
{
	// TODO:
	// Get the released resources or resume the operations that were paused or stopped in OnScreenOff().
}

void
SleepCatcher::OnScreenOff (void)
{
	// TODO:
	//  Unless there is a strong reason to do otherwise, release resources (such as 3D, media, and sensors) to allow the device to enter the sleep mode to save the battery.
	// Invoking a lengthy asynchronous method within this listener method can be risky, because it is not guaranteed to invoke a callback before the device enters the sleep mode.
	// Similarly, do not perform lengthy operations in this listener method. Any operation must be a quick one.
}

